﻿#include "xplaneConnect.h"

#ifdef __cplusplus
extern "C" {
#endif

    // 强制NVIDIA显卡渲染
    __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;

    // 强制AMD显卡渲染
    __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;

#ifdef __cplusplus
}
#endif

#include "ADF.h"
#include "Thread.h"
#include <algorithm>
#include <thread>
#include <windows.h>

//void drefThreadFunction(XPCSocket sock, float* vor1hdef, int* sizedf1, float* vor1dr, int* sizedr1) {
//    getDREF(sock, "sim/cockpit/radios/nav1_hdef_dot", vor1hdef, sizedf1);
//    getDREF(sock, "sim/cockpit/radios/nav1_fromto", vor1dr, sizedr1);
//}

int main()
{
    int height, width;
    // 禁用系统DPI缩放，获取物理分辨率
    SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);

    setcaption("ADF");
    width = GetSystemMetrics(SM_CXSCREEN);
    height = GetSystemMetrics(SM_CYSCREEN);
    float newWidth = 2.5 * width / 4.0;
    float newHeight = 3 * height / 4.0;
    initgraph(newWidth, newHeight, INIT_RENDERMANUAL);

    const char* IP = MYIP;      //IP Address of computer running X-Plane
    XPCSocket sock = openUDP(IP);
    //float vor1hdef = 0, vor1dr = 0;
    //int sizedf1 = 1, sizedr1;


    float scale = 1.0f; // 缩放比例变量
    bool rightButtonDown = false; // 右键状态跟踪
    float offsetX = 0.0f, offsetY = 0.0f; // 新增视角偏移量
    const float MOVE_SPEED = 5.0f; // 移动速度（像素/帧）


    for (; is_run(); delay_fps(60)) {
        cleardevice();
        setbkcolor(EGEARGB(255, 105, 103, 101));
        //std::thread drefThread(drefThreadFunction, sock, &vor1hdef, &sizedf1, &vor1dr, &sizedr1);
        //drefThread.join();

        // 处理所有鼠标消息
        while (mousemsg()) {
            mouse_msg msg = getmouse();

            // 更新右键状态
            if (msg.is_right()) {
                if (msg.is_down()) rightButtonDown = true;
                if (msg.is_up()) rightButtonDown = false;
            }

            // 处理滚轮缩放
            if (rightButtonDown && msg.is_wheel()) {
                // 每单位滚轮调整10%的缩放
                scale += msg.wheel * 0.001f;
                // 限制缩放范围（0.1x ~5x）
                scale = std::clamp(scale, 0.5f, 10.0f);
            }
        }

        // 新增键盘处理部分
        if (keystate(VK_LEFT))  offsetX -= MOVE_SPEED;
        if (keystate(VK_RIGHT)) offsetX += MOVE_SPEED;
        if (keystate(VK_UP))    offsetY -= MOVE_SPEED;
        if (keystate(VK_DOWN))  offsetY += MOVE_SPEED;

        // 限制偏移范围（可选）
        const float MAX_OFFSET = 200.0f;
        offsetX = std::clamp(offsetX, -MAX_OFFSET, MAX_OFFSET);
        offsetY = std::clamp(offsetY, -MAX_OFFSET, MAX_OFFSET);

        // 修改绘制位置（添加偏移量）
        drawADF(newWidth / 2.0f - offsetX,
            newHeight / 2.0f - offsetY,
            newHeight * 0.4f * scale, 0);

    }
    getch();
    closegraph();
}